package com.brillilab.service.core.elsearch.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.brillilab.common.utils.MyBeanUtils;
import com.brillilab.domain.po.elsearch.EsReagent;
import com.brillilab.domain.po.elsearch.EsReagentAttr;
import com.brillilab.domain.po.elsearch.EsSample;
import com.brillilab.domain.po.elsearch.EsSampleAttr;
import com.brillilab.domain.po.kit.Kit;
import com.brillilab.domain.po.kit.Reagent;
import com.brillilab.domain.po.kit.ReagentAntibody;
import com.brillilab.domain.po.kit.ReagentPlasmid;
import com.brillilab.domain.po.kit.ReagentPrimer;
import com.brillilab.domain.po.kit.SampleAttr;
import com.brillilab.service.core.elsearch.IEsKitSaveService;
import com.brillilab.service.core.elsearch.IEsReagentService;
import com.brillilab.service.core.elsearch.IEsSampleService;
import com.brillilab.service.core.kit.IKitService;
import com.brillilab.service.core.kit.IReagentAntibodyService;
import com.brillilab.service.core.kit.IReagentPlasmidService;
import com.brillilab.service.core.kit.IReagentPrimerService;
import com.brillilab.service.core.kit.IReagentService;
import com.brillilab.service.core.kit.ISampleAttrService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class EsKitSaveServiceImpl implements IEsKitSaveService {
	@Autowired
	private IEsReagentService esReagentService;
	@Autowired
	private IEsSampleService esSampleService;
	@Autowired
	private IKitService kitService;
	@Autowired
	private IReagentService reagentService;
	@Autowired
	private IReagentAntibodyService reagentAntibodyService;
	@Autowired
	private IReagentPrimerService reagentPrimerService;
	@Autowired
	private IReagentPlasmidService reagentPlasmidService;
	@Autowired
	private ISampleAttrService sampleAttrService;

	
	@Override
//	@Async
	public void kitSave(Long kitId) {
		Kit kit = kitService.selectById(kitId);
		if(kit!=null && kit.getClassify()==1) {
			this.reagentSave(kit);
		}else if(kit!=null && kit.getClassify()==2) {
			this.sampleSave(kit);
		}
	}

	private void reagentSave(Kit kit) {
		EsReagent eb = MyBeanUtils.copyBean(kit, EsReagent.class);
		eb.setKitId(kit.getId());
		eb.setKitName(kit.getName());
		List<EsReagentAttr> esReagentList = new ArrayList<EsReagentAttr>();
		List<Reagent> reagentList = reagentService.selectList(kit.getId());
		reagentList.forEach(reagent -> {
			EsReagentAttr attr = new EsReagentAttr();
			attr.setReagentId(reagent.getId());
			attr.setIsDelete(reagent.getIsDelete());
			attr = this.antibodyConverter(attr, kit.getId(), reagent.getId());
			attr = this.primerConverter(attr, kit.getId(), reagent.getId());
			attr = this.plasmidConverter(attr, kit.getId(), reagent.getId());
			esReagentList.add(attr);
		});
		eb.setReagents(esReagentList);
		esReagentService.save(eb);
	}
	
	private void sampleSave(Kit kit) {
		EsSample eb = MyBeanUtils.copyBean(kit, EsSample.class);
		eb.setKitId(kit.getId());
		eb.setKitName(kit.getName());
		List<EsSampleAttr> sampleAttrList = new ArrayList<EsSampleAttr>();
		List<SampleAttr> attrList = sampleAttrService.selectList(kit.getId());
		if(attrList!=null && attrList.size()>0) {
			attrList.forEach(attr ->{
				EsSampleAttr esAttr = new EsSampleAttr();
				esAttr.setSampleTemplateId(attr.getSampleTemplateId());
				esAttr.setAttrValue(attr.getAttrValue());
				sampleAttrList.add(esAttr);
			});
			eb.setSampleAttr(sampleAttrList);
		}
		esSampleService.save(eb);
	}

	private EsReagentAttr plasmidConverter(EsReagentAttr attr, Long kitId, Long reagentId) {
		ReagentPlasmid plasmid = reagentPlasmidService.selectOne(kitId, reagentId);
		if(plasmid!=null) {
			attr.setSkeletonPlasmidName(plasmid.getSkeletonPlasmidName());
			attr.setPromoter(plasmid.getPromoter());
			attr.setClassify(plasmid.getClassify());
			attr.setProkaryoticResistance(plasmid.getProkaryoticResistance());
			attr.setEukaryoticResistance(plasmid.getEukaryoticResistance());
			attr.setLabel(plasmid.getLabel());
			attr.setClonedHost(plasmid.getClonedHost());
			attr.setExpressionHost(plasmid.getExpressionHost());
			attr.setCopyLevel(plasmid.getCopyLevel());
			attr.setPlasmidType(plasmid.getPlasmidType());
			attr.setConstitutive(plasmid.getConstitutive());
			attr.setInductionMethod(plasmid.getInductionMethod());
			attr.setClonedMethod(plasmid.getClonedMethod());
		}
		return attr;
	}

	
	
	private EsReagentAttr primerConverter(EsReagentAttr attr, Long kitId, Long reagentId) {
		ReagentPrimer primer = reagentPrimerService.selectOne(kitId, reagentId);
		if(primer!=null) {
			attr.setPrimerName(primer.getName());
			attr.setApplicationPr(primer.getApplication());
			attr.setSpecies(primer.getSpecies());
			attr.setRegion(primer.getRegion());
		}
		return attr;
	}
	
	
	private EsReagentAttr antibodyConverter(EsReagentAttr attr, Long kitId, Long reagentId) {
		ReagentAntibody antibody = reagentAntibodyService.selectOne(kitId, reagentId);
		if(antibody!=null) {
			attr.setCloneNo(antibody.getCloneNo());
			attr.setAntigenTarget(antibody.getAntigenTarget());
			attr.setApplicationAn(antibody.getApplication());
			attr.setHostSource(antibody.getHostSource());
			attr.setReactivity(antibody.getReactivity());
			attr.setCoupling(antibody.getCoupling());
			attr.setCloneType(antibody.getCloneType());
		}
		return attr;
	}

}
